import express from 'express';
import jwt from 'jsonwebtoken'//用于加密
import * as dotenv from 'dotenv'
dotenv.config({
    path: ['.env.local','.env']
})
const app = express()
const port = 3000
async function authKlingai() {//校验
    const headers = {//加密
        algorithm: 'HS256'
    }
    const now = Math.floor(Date.now() / 1000)
    const payload = {//数据
        iss: process.env.ACCESS_KEY_ID,
        exp: now + 1800,
        nbf: now-5,
    }
    //用kling的密钥生成一个令牌
    const token = jwt.sign(payload, process.env.ACCESS_KEY_SECRET, headers)
    return token
}
//定义了一个get接口
app.get('/jwt-auth', async (req, res) => {
    const token = await authKlingai()
    res.send(token)
})
app.listen(port, () => {
    console.log(`Example app listening at http://localhost:${port}`)
})
